﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Runtime;
using Galaktika.BI.Runtime.Services;
using System.ComponentModel;

namespace Galaktika.BI.Silverlight.Controls
{
    public enum SimpleEditorType
    {
        TextBox,
        Memo,
        Number,
        DateTime
    }

    public partial class SimpleEditor : UserControl, IBindableObject, IEditableObject
    {
        public SimpleEditor()
        {
            InitializeComponent();
        }

        string m_EditorType = SimpleEditorType.Memo.ToString();
        [DefaultValue("Memo")]
        public string EditorType
        {
            get
            {
                return m_EditorType;
            }
            set
            {
                m_EditorType = value;
            }
        }

        bool m_IsReadonly = false;
        [DefaultValue(false)]
        public bool IsReadonly
        {
            get
            {
                return m_IsReadonly;
            }
            set
            {
                m_IsReadonly = value;
            }
        }

        [DefaultValue(null)]
        object m_Value = null;
        public object Value
        {
            get
            {
                return m_Value;
            }
            set
            {
                m_Value = value;
            }
        }

        public object GetCurrentValue()
        {
            if (LayoutRoot.Children.Count > 0)
            {
                Control edit = LayoutRoot.Children[0] as Control;
                //Control ctrl = null;
                SimpleEditorType type = (SimpleEditorType)Enum.Parse(typeof(SimpleEditorType), m_EditorType, true);
                switch (type)
                {
                    case SimpleEditorType.Memo:
                        return (edit as RichTextBox).Text;
                    case SimpleEditorType.DateTime:
                        if (m_Value == null)
                        {
                            m_Value = DateTime.Now;
                        }
                        (edit as System.Windows.Controls.DatePicker).SelectedDate = m_Value as DateTime?;
                        return m_Value;
                    case SimpleEditorType.Number:
                        return (edit as NumericUpDown).Value;
                    default:
                        return (edit as TextBox).Text;
                }
            }

            return null;
        }

        private void UpdateView()
        {
            LayoutRoot.Children.Clear();
            Control ctrl = null;
            SimpleEditorType type = (SimpleEditorType)Enum.Parse(typeof(SimpleEditorType), m_EditorType, true);
            switch (type)
            {
                case SimpleEditorType.Memo:
                    ctrl = new RichTextBox();
                    (ctrl as RichTextBox).VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                    (ctrl as RichTextBox).TextWrapping = TextWrapping.Wrap;
                    (ctrl as RichTextBox).AcceptsReturn = true;
                    break;
                case SimpleEditorType.DateTime:
                    ctrl = new System.Windows.Controls.DatePicker();
                    if (m_Value == null)
                    {
                        m_Value = DateTime.Now;
                    }
                    (ctrl as System.Windows.Controls.DatePicker).SelectedDate = (m_Value as DateTime?);
                    break;
                case SimpleEditorType.Number:
                    ctrl = new NumericUpDown();
                    (ctrl as NumericUpDown).Maximum = double.MaxValue;
                    break;
                default:
                    ctrl = new TextBox();
                    (ctrl as TextBox).IsReadOnly = m_IsReadonly;
                    (ctrl as TextBox).Text = m_Value == null ? "" : m_Value.ToString();
                    break;
            }

            //ctrl.EditValue = m_Value;
            
            LayoutRoot.Children.Add(ctrl);
        }

        #region IBindableObject Members

        public IBindingContext Context { get; set; }

        private IDictionary<string, IBindingManager> m_Bindings;
        public IDictionary<string, IBindingManager> Bindings
        {
            get
            {
                if (m_Bindings == null)
                {
                    m_Bindings = new SafeDictionary<string, IBindingManager>();
                }

                return m_Bindings;
            }
        }

        #endregion

        #region IEditableObject Members

        public void BeginEdit()
        {
            
        }

        public void CancelEdit()
        {
            
        }

        public void EndEdit()
        {
            this.UpdateView();
            if (!this.Context.UserData.ContainsKey(this.Name))
            {
                this.Context.UserData[this.Name] = this;
            }
        }

        #endregion
    }
}
